package com.yunwushanhe.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunwushanhe.document.entity.Users;
import com.yunwushanhe.document.mapper.UsersMapper;
import com.yunwushanhe.document.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 云雾山河
 * @since 2022-10-09
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
    @Autowired
    UsersMapper usersMapper;
    Users users;

    public Users getUsers(){
        return this.users;
    }

    /**
     * 登陆时查询数据库匹配账号和密码的操作
     * @param userNumber
     * @param password
     * @return
     */
    @Override
    public Users getUserByNumberAndPassword(String userNumber,String password) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("user_number",userNumber);
        wrapper.eq("password",password);
        Users user = usersMapper.selectOne(wrapper);
        this.users = user;
        return user;
    }

    @Override
    public Users getUserNumber(String userNumber) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("user_number",userNumber);
        Users user = usersMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public void insertUser(Users user) {
        usersMapper.insert(user);
    }

    @Override
    public void updateUser(Users user) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username",users.getUsername());
        wrapper.eq("user_number",users.getUserNumber());
        wrapper.eq("password",users.getPassword());
        usersMapper.update(user,wrapper);
        this.users = user;
    }

    @Override
    public void retrieveUser(Users user) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("user_number",user.getUserNumber());
        usersMapper.update(user,wrapper);
    }
}
